﻿using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Windows.Forms;
using CircleCut.Models;
using SGH;

namespace CircleCut.Controllers
{
    public class HomeController : Controller
    {
        private InputDBContext db = new InputDBContext();
        public static List<CircleCut.Models.Output> OutList = new List<CircleCut.Models.Output>();
        public  static System.Drawing.Bitmap bmp;
        

        public static List<Common.Models.Input> In = new List<Common.Models.Input>();
        public static List<Common.Models.Output> Out = new List<Common.Models.Output>();

        

        void SGHCalc(int patternType)
        {

            if (In.Count < 1)
            {
                MessageBox.Show("没有输入数据！");
                return;
            }

            var paras = "";
            var task = Task.Run(() =>
            {

            Parallel.For(0, In.Count, i =>  new SGH.Program().Start(In[i], patternType, ref Out, paras)
                );
            });
            task.ContinueWith(o =>
            {
                MessageBox.Show("SGH计算完成");
                //这里执行计算完成之后的功能
                var a = Out.Count - 1;//取最后一个out记录的索引
                var b = Out[a];
                bmp = Draw.OutputToBitmap(b);//将output转化为图片
               

            });

        }



        public ActionResult Index()
        {            
            return View();
        }



        public ActionResult TestForOne()
        {   
            //输入转格式
            var a1 = InputsController.InList;
            
            //判断是否选择了输入
            if (a1==null|| a1.Count != 1)
            {
                return Content("需求输入错误");
            }
            /*
            if (a1.Count != 1)
            {
                MessageBox.Show("需求输入错误！");
                return RedirectToAction("Index", "Inputs");
            }
            */
            string b1 = '[' + a1[0].Length + ',' + a1[0].Width + ',' + a1[0].DiaTotalNum + ']';

            //毛坯转格式
            var a2 = CirclesController.CirList;
            if (a2.Count != int.Parse(a1[0].DiaTotalNum))
            {
                MessageBox.Show("毛坯输入错误！");
                return RedirectToAction("Index","Circles");
            }

            string b2 = "";
            var i = 0;
            for (int j = 0; j < a2.Count; j++)
            {
                 b2 += "\r\n[" + a2[i].Diameter + ',' + a2[i++].Demand + ']';
            }

            //string c = "[2000, 1000, 4]\r\n[350, 200]\r\n[250, 150]\r\n[220, 300]\r\n[330, 250]"
            string b3 = b2;
            string c = b1 + b3;

            string d = c;
            ReadInput.ReadCalc(d, ref In);
            SGHCalc(0);

            return View("Index");
        }

        public ActionResult Test()
        {
            //输入转格式
            var a1 = InputsController.InList;
            string b1 = '[' + a1[0].Length + ',' + a1[0].Width + ',' + a1[0].DiaTotalNum + ']';

            //毛坯转格式
            var a2 = CirclesController.CirList;
            string b2 = "";
            var i = 0;
            for (int j = 0; j < a2.Count; j++)
            {
                b2 += "\r\n[" + a2[i].Diameter + ',' + a2[i++].Demand + ']';
            }

            //string c = "[2000, 1000, 4]\r\n[350, 200]\r\n[250, 150]\r\n[220, 300]\r\n[330, 250]"
            string b3 = b2;
            string c = b1 + b3;

            string d = c;
            ReadInput.ReadCalc(d, ref In);
            SGHCalc(1);

            return View("Index");
        }

        public ActionResult Sgh()
        {
            //输入转格式
            var a1 = InputsController.InList;
            string b1 = '[' + a1[0].Length + ',' + a1[0].Width + ',' + a1[0].DiaTotalNum + ']';

            //毛坯转格式
            var a2 = CirclesController.CirList;
            string b2 = "";
            var i = 0;
            for (int j = 0; j < a2.Count; j++)
            {
                b2 += "\r\n[" + a2[i].Diameter + ',' + a2[i++].Demand + ']';
            }

            //string c = "[2000, 1000, 4]\r\n[350, 200]\r\n[250, 150]\r\n[220, 300]\r\n[330, 250]"
            string b3 = b2;
            string c = b1 + b3;

            string d = c;
            ReadInput.ReadCalc(d, ref In);
            SGHCalc(2);

            return View("Index");
        }

      public ActionResult Output([Bind(Include = "ID,Number,PatternNum,TotalPlateNum,delta,beta,e,p,UtilizationRate,Time")] Output output)
        {

            //output = new CircleCut.Models.Output();
            output.Number = InputsController.InList[0].Number;
            var a = Out.Count - 1;
            int b1 = Out[a].PatternNum; ;
            output.PatternNum = b1.ToString();

            int b2 = Out[a].TotalPlateNum;
            output.TotalPlateNum = b2.ToString();

            int b3 = 5;
            output.delta = b3.ToString();

            double b4 = 4.22;
            output.beta = b4.ToString();

            double b5 = Constant.emixilo;
            output.e = b5.ToString();

            double b6 = Constant.p;
            output.p = b6.ToString();

            double b7 = Out[a].UtilizationRate;
            output.UtilizationRate = b7.ToString();

            TimeSpan b8 = Constant.dt;
            output.Time = b8.ToString();

            db.Outputs.Add(output);
            db.SaveChanges();

            return RedirectToAction("Index","Outputs");

            }                          

    }
}